home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / checkbox / registries / gconf.py < prev    next >
Encoding:
Python Source  |  2009-04-27  |  3.6 KB  |  123 lines

  1. #
  2. # This file is part of Checkbox.
  3. #
  4. # Copyright 2008 Canonical Ltd.
  5. #
  6. # Checkbox is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # Checkbox is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Checkbox.  If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import re
  20.  
  21. from checkbox.lib.conversion import string_to_type
  22.  
  23. from checkbox.properties import Path, String
  24. from checkbox.registries.command import CommandRegistry
  25. from checkbox.registries.data import DataRegistry
  26.  
  27.  
  28. class SourceRegistry(DataRegistry):
  29.  
  30.     def items(self):
  31.         items = []
  32.         lines = []
  33.  
  34.         id = depth = None
  35.         for line in self.split("\n"):
  36.             if not line:
  37.                 continue
  38.  
  39.             match = re.match(r"(\s+(\/)?)(.+)", line)
  40.             if not match:
  41.                 lines[-1] += line
  42.                 continue
  43.  
  44.             space = len(match.group(1).rstrip("/"))
  45.             if depth is None:
  46.                 depth = space
  47.  
  48.             if space > depth:
  49.                 lines.append(line)
  50.             elif match.group(2) is not None:
  51.                 if id is not None:
  52.                     value = SourceRegistry("\n".join(lines))
  53.                     lines = []
  54.  
  55.                     items.append((id, value))
  56.  
  57.                 id = match.group(3).split("/")[-1].rstrip(":")
  58.             else:
  59.                 (key, value) = match.group(3).split(" = ", 1)
  60.                 if value == "(no value set)":
  61.                     value = None
  62.                 else:
  63.                     match = re.match(r"\[([^\]]*)\]", value)
  64.                     if match:
  65.                         list_string = match.group(1)
  66.                         if len(list_string):
  67.                             value = list_string.split(",")
  68.                         else:
  69.                             value = []
  70.                     else:
  71.                         value = string_to_type(value)
  72.  
  73.                 items.append((key, value))
  74.  
  75.         if lines:
  76.             value = SourceRegistry("\n".join(lines))
  77.             items.append((id, value))
  78.  
  79.         return items
  80.  
  81.  
  82. class GconfRegistry(CommandRegistry):
  83.     """Registry for gconf information.
  84.  
  85.     Each item contained in this registry consists of the udi as key and
  86.     the corresponding device registry as value.
  87.     """
  88.  
  89.     # Configuration source to use for a user.
  90.     source = Path(default="~/.gconf")
  91.  
  92.     # Command to retrieve gconf information.
  93.     command = String(default="gconftool-2 -R / "
  94.         "--config-source xml:readwrite:$source")
  95.  
  96.     def __init__(self, *args, **kwargs):
  97.         super(GconfRegistry, self).__init__(*args, **kwargs)
  98.  
  99.         self.command = self.command.replace("$source", self.source)
  100.  
  101.     def items(self):
  102.         items = []
  103.  
  104.         key = None
  105.         lines = []
  106.         for line in self.split("\n"):
  107.             if line.startswith(" /"):
  108.                 if lines:
  109.                     value = SourceRegistry("\n".join(lines))
  110.                     items.append((key, value))
  111.                 key = line.strip().lstrip("/").rstrip(":")
  112.             else:
  113.                 lines.append(line)
  114.  
  115.         if lines:
  116.             value = SourceRegistry("\n".join(lines))
  117.             items.append((key, value))
  118.  
  119.         return items
  120.  
  121.  
  122. factory = GconfRegistry
  123.